import auditService from "@/service/common/audit.service";
import finance_tackleService from "@/service/busi/finance/finance_tackle.service";
import { numberC, jsonCopy, confirmC, alertC, isValue } from '@/util/common'
export const financeDealMixin = {
  methods: {
    // 单独的保存财务信息
    // params  审核参数信息
    // financeData  财务信息
    // fromtype  单据来源类型，枚举：1-借款 2-还款 3-报销
    // showMsg  是否显示提示信息
    financeDeal(params, financeData = [], fromtype, config = { showMsg: true, msg: '确认保存?'}) {
      let tData = jsonCopy(financeData)
      if(tData.length == 0) {
        alertC("请先录入财务信息!");
        return Promise.reject();
      }
      let isInputTypeOfFunds = financeData.some(i => !isValue(i['typeOfFunds']));
      if(isInputTypeOfFunds) {
        alertC("请选择经费类型!");
        return Promise.reject();
      }

      // 预算资金，经济科目必选
      // ['31', '32', '1'].includes(value + '')
      for(let i = 0; i < tData.length; i++) {
        const item = tData[i];
        if(item['sourcefunds'] == 1 && !isValue(item['fkeconomicdept'])) {
          alertC("资金来源为【预算资金】时;经济科目不能为空!");
          return Promise.reject();
        }
        // 预算资金，暂收，暂付，必选相关信息
        if(['1', '31', '32'].includes(item['sourcefunds'] + '')) {
          if(item['sourcefundsrk'] == '点击选择相关信息' || !isValue(item['sourcefundsrk'])) {
            alertC("请将相关信息选择完毕!");
            return Promise.reject();
          }
        }

        // 市局经费，且资金来源为预算资金时，只能选部门为市局的预算
        let sourcefundsrk = item['sourcefundsrk'].split('-');
        if(item.typeOfFunds == 2 && item.sourcefunds == 1 && sourcefundsrk[0] != '市局') {
          alertC("经费类型为【市局经费】且资金来源为【预算资金】时只能选择部门为市局的预算！")
          return Promise.reject();
        }
        // 部门经费，且资金来源为预算资金时，不能选部门为市局的预算
        else if(item.typeOfFunds == 1 && item.sourcefunds == 1 && sourcefundsrk[0] == '市局') {
          alertC("经费类型为【部门经费】且资金来源为【预算资金】时不能选择部门为市局的预算！")
          return  Promise.reject();
        }
      }

      tData.forEach(i => {
        i.budgetyear = new Date().getFullYear()
        i.fkfromid = params['busId']; // 业务主表主键
        i.fromtype = fromtype; // 单据来源类型，枚举：1-借款 2-还款 3-报销
        if(i.sourcefundsrk == '点击选择相关信息') {
          i.sourcefundsrk = ''
        }
      })
      console.log("🚀 ~ financeDeal ~ tData:", tData)

      return new Promise((resolve, reject) => {
        confirmC(config.msg).then(() => {
          finance_tackleService.add(tData).then(res => {
            if(res.data.code == 0) {
              if(config.showMsg) {
                alertC(res.data.msg, "success");
              }
              resolve(res)
            }
            else {
              alertC(res.data.msg, "warning");
              reject()
            }
          })
          .catch(() =>  reject())
        })
        .catch(err => reject())
      })
    },

    // 财务处理，及审核通过
    financeDealAudit(params, financeData = [], fromtype) {
      let tData = jsonCopy(financeData)
      if(tData.length == 0) {
        alertC("请先录入财务信息!");
        return Promise.reject();
      }
      tData.forEach(i => {
        i.budgetyear = new Date().getFullYear()
        i.fkfromid = params['busId']; // 业务主表主键
        i.fromtype = fromtype; // 单据来源类型，枚举：1-借款 2-还款 3-报销
      })

      const tform = jsonCopy(params);
      tform.content = "已处理。"
      if(tData.length != 0) {
        tform.extra = {
          busiTPaybudgetList: tData
        };
      }
      return new Promise((resolve, reject) => {
        confirmC('确认保存并审核通过?').then(() => {
          auditService.approve(tform).then(res => {
            if(res.data.code == 0) {
              alertC(res.data.msg, "success");
              resolve(res)
            }
            else {
              alertC(res.data.msg, "warning");
              reject()
            }
          })
          .catch(() =>  reject())
        })
        .catch(err => reject())
      })
    },

    // 获取已经财务处理过的财务信息
    getFinanceInfo(keyid) {
    }
  },
  computed: {
    // 是否是财务处理
    financeRole() {
      // 判断是否是财务会计初审，财务处理审核
      return this.$store.getters['loginStore/getUserInfo']['attributeValue'].includes('3')
    },

    // 是否该我审核
    isMeAudit(params) { 
      return (this.currentData || {})['fkaudituserid'] == this.$store.getters['loginStore/getUserInfo']['userId']
    }
  },
  mounted() {
  },
}